﻿using System.Web.Security;
using Eneta.Portal.Common;

namespace Eneta.Portal.Authentication.Live
{
    class LiveRoleProvider : RoleProvider
    {
        protected string AppName { get; set; }

        /// <summary>
        /// Initialize the provider, configuration can be passed in but none is required
        /// </summary>
        /// <param name="name">Name that is configured for the provider</param>
        /// <param name="config">NameValue collection of configuration arguments</param>
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (string.IsNullOrEmpty(name))
                name = "LiveRoleProvider";

            AppName = name;
            base.Initialize(name, config);
        }

        /// <summary>
        /// Not implemented - the LiveRoleProvider is meant just to provide Authenticated Live Users
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
        }

        public override string ApplicationName
        {
            get
            {
                return AppName;
            }
            set
            {
                AppName = value;
            }
        }

        /// <summary>
        /// Not implemented - the LiveRoleProvider is meant just to provide Authenticated Live Users
        /// </summary>
        /// <param name="roleName"></param>
        public override void CreateRole(string roleName)
        {
        }

        /// <summary>
        /// Not implemented - the LiveRoleProvider is meant just to provide Authenticated Live Users
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="throwOnPopulatedRole"></param>
        /// <returns></returns>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            return true;
        }

        /// <summary>
        /// Simple implementation checks if the role is Authenticated Live Users and
        /// then checks if the _user has a Live profile.  If so it returns true.  This
        /// allows users to add Authenticated Live Users to a site and allow access
        /// to anyone using Live ID
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="usernameToMatch"></param>
        /// <returns></returns>
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            Logger.LogInfo("FindUsersInRole() ","LiveRoleProvider");

            var roles = new string[1];

            if (roleName.ToLower() == "authenticated live users")
            {
                var bup = BlogUserProfile.GetUser(usernameToMatch);
                if (bup != null)
                {
                    roles[0] = bup.Id;
                }
            }
            else
            {
                roles[0] = "";
            }

            return roles;
        }

        /// <summary>
        /// Only returns Authenticated Live Users, this is the only role this provider knows
        /// </summary>
        /// <returns></returns>
        public override string[] GetAllRoles()
        {
            Logger.LogInfo("GetAllRoles() ", "LiveRoleProvider");
            string[] roles = new string[1];
            roles[0] = "Authenticated Live Users";
            return roles;
        }

        /// <summary>
        /// Always and only returns Authenticated Live Users
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public override string[] GetRolesForUser(string username)
        {
            // Only comment in when debugging provider. This method is used heavily.
            //Logger.LogInfo("GetRolesForUser() ", "LiveRoleProvider");
            return new[] {"Authenticated Live Users"};
        }

        /// <summary>
        /// Returns a list of all known Live users if the Authenticated Live Users role
        /// is checked
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override string[] GetUsersInRole(string roleName)
        {
            Logger.LogInfo("GetUsersInROle() ", "LiveRoleProvider");
            string[] roles;

            if (roleName.ToLower() == "authenticated live users")
            {
                BlogUserProfile[] bup = BlogUserProfile.GetAllUsers();
                roles = new string[bup.Length];

                for (int i = 0; i < bup.Length; i++)
                {
                    roles[i] = bup[i].Id;
                }
            }

            roles = new string[1];
            roles[0] = "";
            return roles;
        }

        /// <summary>
        /// Always returns true if the _user is checked for Authenticated Live Users.
        /// Always returns false otherwise.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            Logger.LogInfo("IsUserInRole() ", "LiveRoleProvider");
            if (roleName.ToLower() == "authenticated live users")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Not implemented - the LiveRoleProvider is meant just to provide Authenticated Live Users
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
        }

        /// <summary>
        /// Only returns true for Authenticated Live Users
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool RoleExists(string roleName)
        {
            Logger.LogInfo("RoleExists() ", "LiveRoleProvider");
            if (roleName.ToLower() == "authenticated live users")
            {
                return true;
            }

            return false;
        }
    }

}
